﻿using System;

namespace Portkit.Core
{
    public class Lazy<T>
    {
        private readonly Func<T> _factory;
        private T _value;

        public bool IsValueCreated { get; protected set; }

        public T Value
        {
            get
            {
                if (IsValueCreated || _factory == null)
                {
                    return _value;
                }
                _value = _factory();
                IsValueCreated = true;
                return _value;
            }
        }

        public Lazy(Func<T> factory)
        {
            _factory = factory;
        }
    }
}
